﻿namespace Hidistro.UI.AccountCenter.CodeBehind
{
    using ASPNET.WebControls;
    using Hidistro.Core;
    using Hidistro.Entities;
    using Hidistro.Membership.Context;
    using Hidistro.UI.Common.Controls;
    using Hishop.Components.Validation;
    using System;
    using System.Collections.Generic;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;
    using Hidistro.UI.SaleSystem.Tags;
    using wnxd.Web;
    using System.Web.UI;
    using System.Linq;
    using System.Collections;
    using System.Text.RegularExpressions;

    public class UserProfile_new : MemberTemplatedWebControl
    {
        private HtmlGenericControl UserName;
        private wnxd_User_Gender wnxdGender;
        private HtmlInputText calendDate;//WebCalendar
        private HtmlInputText QQ;
        private HtmlInputText Email;
        //private RegionSelector dropRegions;
        private RegionNewSelector dropRegions;
        private HtmlInputText Address;
        private HtmlInputText RealName;
        private HtmlInputText CellPhone;
        private HtmlInputText TelPhone;
        private MonthlyIncomeSelector dropMI;
        private WedSelector wedselector;
        private HtmlInputHidden hfval;
        protected override void AttachChildControls()
        {
            PageTitle.AddSiteNameTitle("个人信息");
            this.UserName = (HtmlGenericControl)this.FindControl("UserName");
            this.wnxdGender = (wnxd_User_Gender)this.FindControl("wnxdGender");
            this.calendDate = (HtmlInputText)this.FindControl("calendDate");//WebCalendar
            this.QQ = (HtmlInputText)this.FindControl("QQ");
            this.Email = (HtmlInputText)this.FindControl("Email");
            //this.dropRegions = (RegionSelector)this.FindControl("dropRegions");
            this.dropRegions = (RegionNewSelector)this.FindControl("dropRegionsnew");// ("dropRegionsnew");
            this.Address = (HtmlInputText)this.FindControl("Address");
            this.RealName = (HtmlInputText)this.FindControl("RealName");
            this.CellPhone = (HtmlInputText)this.FindControl("CellPhone");
            this.TelPhone = (HtmlInputText)this.FindControl("TelPhone");
            this.dropMI = (MonthlyIncomeSelector)this.FindControl("MonthlyIncome");
            this.wedselector = (WedSelector)this.FindControl("WedSelector");
            this.hfval = (HtmlInputHidden)this.FindControl("hfval");// ("hfval");
            if (!this.Page.IsPostBack)
            {
                Member user = HiContext.Current.User as Member;
                this.UserName.InnerText = user.Username;
                this.wnxdGender.Gender = user.Gender;
                if (user.BirthDate.HasValue ? (user.BirthDate.GetValueOrDefault() > DateTime.MinValue) : false)
                {
                    //this.calendDate.SelectedDate = user.BirthDate;
                    this.calendDate.Value = user.BirthDate.Value.ToString("yyyy-MM-dd").ToString();
                }
                this.QQ.Value = Globals.HtmlDecode(user.QQ);
                this.Email.Value = Globals.HtmlDecode(user.Email);
                this.dropRegions.SetSelectedRegionId(user.RegionId);
                this.Address.Value = Globals.HtmlDecode(user.Address);
                this.RealName.Value = Globals.HtmlDecode(user.RealName);
                this.CellPhone.Value = Globals.HtmlDecode(user.CellPhone);
                this.TelPhone.Value = Globals.HtmlDecode(user.TelPhone);
                if (!string.IsNullOrEmpty(user.MonthlyIncome))
                    this.dropMI.SetSelectedMI(int.Parse(user.MonthlyIncome));//user.MonthlyIncome
                if (!string.IsNullOrEmpty(user.Wed.ToString()))
                    this.wedselector.Wed = user.Wed;
                this.hfval.Value = user.LoveCategory;
            }
        }
        protected override void OnInit(EventArgs e)
        {
            if (this.SkinName == null)
            {
                this.SkinName = "User/Skin-UserProfile_new.html";
            }
            base.OnInit(e);
        }
        protected void Save()
        {
            Member user = Users.GetUser(HiContext.Current.User.UserId, true) as Member;
            IEnumerator ie = this.Controls[0].Controls.OfType<wnxd_User_Menu>().GetEnumerator();
            ie.MoveNext();
            Control col = (Control)ie.Current;
            string Email = Globals.HtmlEncode(this.Page.Request.Form[this.Email.Name]);
            Web wnxd_web = (Web)col.FindControl("wnxd_web");
            if (string.IsNullOrEmpty(Email))
            {
                wnxd_web.AddScript("alert('邮箱不能为空');");
                return;
            }
            user.Gender = this.wnxdGender.Gender;
            DateTime? time = new DateTime?();
            if (!string.IsNullOrEmpty(this.Page.Request.Form["userProfile$calendDate"])) time = DateTime.Parse(this.Page.Request.Form["userProfile$calendDate"]);
            user.BirthDate = time;
            user.QQ = Globals.HtmlEncode(this.Page.Request.Form[this.QQ.Name]);
            if (!this.dropRegions.GetSelectedRegionId().HasValue)
            {
                user.RegionId = 0;
            }
            else
            {
                user.RegionId = this.dropRegions.GetSelectedRegionId().Value;
                user.TopRegionId = RegionHelper.GetTopRegionId(user.RegionId);
            }
            user.Address = this.Page.Request.Form[this.Address.Name];
            user.RealName = this.Page.Request.Form[this.RealName.Name];
            user.CellPhone = this.Page.Request.Form[this.CellPhone.Name];
            user.TelPhone = this.Page.Request.Form[this.TelPhone.Name];
            user.Wed = this.wedselector.Wed;
            if (!this.dropMI.GetSelectedMI().HasValue)
            {
                user.MonthlyIncome = "0";
            }
            else
            {
                user.MonthlyIncome = this.dropMI.GetSelectedMI().Value.ToString();
            }
            user.LoveCategory = this.Page.Request.Form[this.hfval.Name];
            //if (user.RegionId==0)
            //{
            //    wnxd_web.AddScript("alert('配送区域请选择');");
            //    return;
            //}
            //if (string.IsNullOrEmpty(user.Address))
            //{
            //    wnxd_web.AddScript("alert('详细地址不能为空');");
            //    return;
            //}
            //if (string.IsNullOrEmpty(user.RealName))
            //{
            //    wnxd_web.AddScript("alert('收货人不能为空');");
            //    return;
            //}
            //if (string.IsNullOrEmpty(user.CellPhone))
            //{
            //    wnxd_web.AddScript("alert('手机号码不能为空');");
            //    return;
            //}
            //if (!IsInteger(user.CellPhone) || user.CellPhone.Length != 11 || user.CellPhone.IndexOf("1") != 0)
            //{
            //    wnxd_web.AddScript("alert('请输入正确的手机号码');");
            //    return;
            //}
            if (this.ValidationMember(user, wnxd_web))
            {
                if (Users.UpdateUser(user))//if (Users.UpdateUserNew(user, "1", "2", "3")) 
                {
                    //this.ShowMessage("成功的修改了用户的个人资料", true);
                    wnxd_web.AddScript("alert('成功的修改了用户的个人资料');");
                }
                else
                {
                    wnxd_web.AddScript("alert('修改用户个人资料失败');");
                }
            }
        }
        public static bool IsInteger(string s)
        {
            string pattern = @"^\d*$";
            return Regex.IsMatch(s, pattern);
        }
        private bool ValidationMember(Member member, Web wnxd_web)
        {
            ValidationResults results = Hishop.Components.Validation.Validation.Validate<Member>(member, new string[] { "ValMember" });
            string msg = string.Empty;
            if (!results.IsValid)
            {
                foreach (ValidationResult result in (IEnumerable<ValidationResult>)results)
                {
                    msg = msg + Formatter.FormatErrorMessage(result.Message);
                }
                wnxd_web.AddScript("alert('" + msg + "');");
            }
            return results.IsValid;
        }
    }
}

